
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/timer.h>
#include <linux/slab.h>
#include <asm/cacheflush.h>

MODULE_AUTHOR("mdy");
MODULE_DESCRIPTION("demo");
MODULE_LICENSE("GPL");

static int test_size_kb = 1;
static void* test_memory = NULL;

module_param(test_size_kb, int, S_IRUGO | S_IWUSR);

static int __init calc_time_demo_init(void)
{
    int test_size = test_size_kb << 10;
    test_memory = kmalloc(test_size, GFP_KERNEL);

    {   
        struct timespec ts_start, ts_end;
        int* p = (int*)test_memory;
        int i;
        int test_cnt = test_size / 4;

        getnstimeofday(&ts_start);
        for (i = 0; i < test_cnt; i++)
        {
            *p += 1;
            p++;
        }
        flush_cache_all();
        getnstimeofday(&ts_end);

        {
            uint64_t time_ns = (ts_end.tv_sec - ts_start.tv_sec) * 1000000000 + ts_end.tv_nsec - ts_start.tv_nsec;
            printk("test mem size: %d, time count(ns): %lld\n", test_size, time_ns);
        }

    }

    return 0;
}

static void __exit calc_time_demo_exit(void)
{
    if (NULL != test_memory)
    {
        kfree(test_memory);
    }

    return;
}
 
module_init(calc_time_demo_init);
module_exit(calc_time_demo_exit);

